package com.charmpeach.tsss;

import org.apache.http.impl.client.DefaultHttpClient;

import com.charmpeach.tsss.crawler.CourseTag;
import com.charmpeach.tsss.crawler.MessageManger;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.text.Html;
import android.view.GestureDetector;
import android.view.View;
import android.widget.TextView;

public class Assignment extends Activity {
	
	DefaultHttpClient client = null;
	String sessionID = null;
    protected void onCreate(Bundle savedInstanceState) {
    	//StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyDialog().build());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.assignment_layout);
		
    	Intent intent = getIntent();
        final CourseTag tag = new CourseTag(intent.getStringExtra("tag"));
        
        final Handler handler = new Handler()
		{

			public void handleMessage(Message message)
			{
				String tmp[] = ((String) message.obj).split("\\$");
				
				TextView dateView = (TextView)findViewById(R.id.assignment_dateContent);
				TextView contentView = (TextView)findViewById(R.id.assignment_descriptionContent);
				TextView numView = (TextView)findViewById(R.id.assignment_NoContent);
				
				numView.setText(tag.getMessage());
				dateView.setText(tmp[0]);
				contentView.setText(Html.fromHtml(tmp[1]));
			}
		};
        
		Thread thread = new Thread()
		{
			public void run()
			{
				MessageManger mm = MessageManger.getInstance();
				String temp=null;
				try {
					temp = mm.getAssignmentDetail(tag.getCouserID(), tag.getMessage(),Assignment.this);
				} catch (Exception e) {
					// TODO: handle exception
				}
				if(temp == null)
					return;
				Message message = handler.obtainMessage(1, temp);
				handler.sendMessage(message);
			}
			
		};
		
		thread.start();
		
		//test for notification by tj
//		NotificationCompat.Builder mBuilder = 
//				new NotificationCompat.Builder(this)
//				.setSmallIcon(R.drawable.ic_launcher)
//				.setContentTitle("lalala")
//				.setContentText("You just enter assignment");
//		
//		Intent resultIntent = new Intent(this, ListViewLoader.class);
//		PendingIntent resultPendingIntent = 
//				PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//		
//		mBuilder.setContentIntent(resultPendingIntent);
//		
//		int mNotificationID = 001;
//		NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//		mNotifyMgr.notify(mNotificationID, mBuilder.build());
    }
    

}
